package com.feature50.makeover.utils;

import org.apache.log4j.Logger;

import java.awt.*;
import java.awt.event.InputEvent;

public class Platform {
    private static final Logger logger = Logger.getLogger(Platform.class);

    private static Object LCD_HRGB;

    public static boolean APPLE = (System.getProperty("mrj.version") != null);

    public static int mask() {
        return (APPLE) ? InputEvent.META_MASK : InputEvent.CTRL_MASK;
    }

    private static final String JAVA_VERSION = getSystemProperty("java.version");
    public static boolean ISJAVA6 = startsWith(JAVA_VERSION, "1.6");

    public static String getSystemProperty(String key) {
        try {
            return System.getProperty(key);
        }
        catch (SecurityException e) {
            System.err.println("Can't read the System property " + key + ".");
            return null;
        }
    }

    /**
     * Checks the version of the JDK; only checks to the tenths place. E.g., minVersion(1.5.0.13f) will return true
     * if the version is 1.5.0.10.
     *
     * @param version
     * @return
     */
    public static boolean minVersion(float version) {
        try {
            String shortVersion = JAVA_VERSION.substring(0, 3);
            float v = Float.parseFloat(shortVersion);
            if (v >= version) return true;
        } catch (Exception e) {
            logger.warn("Version number not understood (" + JAVA_VERSION + ")", e);
        }
        return false;
    }

    private static boolean startsWith(String str, String prefix) {
        return str != null && str.startsWith(prefix);
    }

    /**
     * Sets anti-aliasing according to various rules. Passing in a Font is recommended, but not required.
     * @param g
     * @param f
     * @return
     */
    public static Graphics2D setAntialiasing(Graphics g, Font f) {
        Graphics2D g2d = (Graphics2D) g;

        if (Platform.minVersion(1.6f)) {
            // turn on sub-pixel AA for everything
            forceSubpixelAntialiasing(g2d);
        } else {
            // previous to Java 6, kerning, ligatures, and sub-pixel AA didn't exist
            if (NativeUtils.getOS() == NativeUtils.OS.Mac) {
                g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            } else if ((f != null) && (f.getSize() > 12)) {
                g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            }
        }

        return g2d;
    }

    public static Graphics2D forceSubpixelAntialiasing(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        if (Platform.minVersion(1.6f)) {
            // turn on sub-pixel AA for everything
            try {
                if (LCD_HRGB == null) LCD_HRGB = RenderingHints.class.getField("VALUE_TEXT_ANTIALIAS_LCD_HRGB").get(RenderingHints.class);
                g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, LCD_HRGB);
            } catch (Exception e) {
                logger.warn("Couldn't get rendering hint for sub-pixel AA", e);
            }
        } else {
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        }

        return g2d;
    }
}
